<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_member_address_set_default_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
        //var_dump($_SESSION); exit();
    
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        //var_dump($_POST); exit();
        $address_id = init_user_data_key($_POST, 'address_id', 'int');
    
        $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
        $member_id = $session_member_info['id'];
         
        //$address_id = 0;            
        if(empty($address_id)){
            $result['status']  = -400;
            $result['message'] = '设置失败';
        }else{
            $model_foreground_member_address = wwy_resource_container::get_model_foreground_member_address();
            //var_dump($model_foreground_member_address); exit();
            $where      = array(
                'id'        => $address_id,
                'member_id' => $member_id,
            );
            $address_row = $model_foreground_member_address->fetch_row($where);
            //var_dump($address_row); exit();
    
            if(empty($address_row)){
                $result['status']  = -400;
                $result['message'] = '设置失败';
            }else{
                $model_foreground_member = wwy_resource_container::get_model_foreground_member();
                $update_time = time();
                $member_info = array(
                    'id'                 => $member_id,                    
                    'default_address_id' => $address_id,
                    'update_time'        => $update_time,
                );
                $update_default_address_result = $model_foreground_member->update_default_address($member_info);
                //var_dump($update_default_address_result); exit();
    
                if(!$update_default_address_result){
                    $result['status']  = -4;
                    $result['message'] = '设置失败';
                }else{
                    $result['status']  = 1;
                    $result['message'] = '设置成功';
                    //$result['data']['default_address_id'] = $address_id;                       
                    self::add_view_data('default_address_id', $address_id);
                }
            }
        }
	
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}